package com.yunsc.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.utils.PageResult;
import com.yunsc.media.service.YunscUploadFileService;
import com.yunsc.store.dao.YunscUserBalanceLogDao;
import com.yunsc.store.dto.QueryUserParamsDTO;
import com.yunsc.store.entity.YunscUserBalanceLogEntity;
import com.yunsc.store.service.YunscUserBalanceLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.yunsc.common.utils.DateUtils.convertStringToSeconds;


@Service("yunscUserBalanceLogService")
public class YunscUserBalanceLogServiceImpl extends ServiceImpl<YunscUserBalanceLogDao, YunscUserBalanceLogEntity> implements YunscUserBalanceLogService {
    @Resource
    private YunscUploadFileService uploadFileService;

    @Override
    public PageResult<YunscUserBalanceLogEntity> queryUserBalanceLog(Integer userId, Integer page) {
        LambdaQueryWrapper<YunscUserBalanceLogEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscUserBalanceLogEntity::getUserId, userId);
        lqw.orderByDesc(YunscUserBalanceLogEntity::getCreateTime);
        Page<YunscUserBalanceLogEntity> pageResult = this.page(new Page<>(page, 15), lqw);
        List<YunscUserBalanceLogEntity> records = pageResult.getRecords();
        return PageResult.formatPageResult(records, page, 15, pageResult.getTotal());

    }


    @Override
    public boolean insertUserBalanceLog(int userId, int scene, String orderNo, String payPrice, int storeId) {
        YunscUserBalanceLogEntity userBalanceLog = new YunscUserBalanceLogEntity();
        userBalanceLog.setUserId(userId);
        // (10用户充值 20用户消费 30管理员操作 40订单退款)
        if (scene == 20){
            userBalanceLog.setDescribe("用户消费："+ orderNo);
            userBalanceLog.setMoney("-" + payPrice);
        }else if (scene == 10){
            userBalanceLog.setDescribe("用户充值");
            userBalanceLog.setMoney("+" + payPrice);
        }else if (scene == 30){
            userBalanceLog.setDescribe("管理员操作");
            userBalanceLog.setMoney(payPrice);
        }else if (scene == 40){
            userBalanceLog.setDescribe("订单退款："+ orderNo);
        }
        userBalanceLog.setStoreId(storeId);
        return this.save(userBalanceLog);
    }

    @Override
    public void addUserBalanceLog(int userId, int scene, String remark, String msg, String payPrice, int storeId) {
        YunscUserBalanceLogEntity userBalanceLog = new YunscUserBalanceLogEntity();
        userBalanceLog.setUserId(userId);
        userBalanceLog.setDescribe(msg);
        userBalanceLog.setRemark(remark);
        userBalanceLog.setMoney(payPrice);
        userBalanceLog.setStoreId(storeId);
        this.save(userBalanceLog);
    }

    @Override
    public PageResult<YunscUserBalanceLogEntity> queryBalanceLog(QueryUserParamsDTO param, Integer storeId) {

        Page<YunscUserBalanceLogEntity> pageResult = this.baseMapper.selectBalanceLogPage(
                new Page<>(param.getPage(), param.getPageSize()),
                param,
                convertStringToSeconds(param.getBetweenTimeStart()),
                convertStringToSeconds(param.getBetweenTimeEnd()),
                storeId
        );
        pageResult.getRecords().forEach(order -> {
            order.getUser().setAvatarUrl(uploadFileService.queryFileUrlById(order.getUser().getAvatarId()));
        });

        return PageResult.formatPageResult(pageResult.getRecords(), param.getPage(), param.getPageSize(), pageResult.getTotal());
    }
}